package com.zzyl.nursing.job;

import com.zzyl.common.utils.spring.SpringUtils;
import com.zzyl.nursing.domain.DeviceData;

import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.service.IDeviceDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Component("deviceDataMigration")
public class DeviceDataMigration {
    @Autowired(required = false)
    private IDeviceDataService deviceDataService;

    public void clean(Integer min) {
        System.out.println("开始执行数据清理任务，参数 min=" + min);

        // 尝试获取依赖
        IDeviceDataService service = getDeviceDataService();
        if (service == null) {
            System.err.println("错误：deviceDataService 未初始化");
            throw new IllegalStateException("deviceDataService 未初始化");
        }

        try {
            System.out.println("调用 deviceDataService.clean(" + min + ")");
            service.clean(min);
            System.out.println("数据清理任务执行完成");
        } catch (Exception e) {
            System.err.println("数据清理任务执行异常: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private IDeviceDataService getDeviceDataService() {
        if (deviceDataService != null) {
            System.out.println("使用注入的 deviceDataService 实例");
            return deviceDataService;
        }

        // 如果自动注入失败，尝试手动获取
        try {
            IDeviceDataService service = SpringUtils.getBean(IDeviceDataService.class);
            System.out.println("通过 SpringUtils 获取到 deviceDataService 实例");
            return service;
        } catch (Exception e) {
            System.err.println("无法从 Spring 上下文获取 IDeviceDataService: " + e.getMessage());
            return null;
        }
    }
}


//    private DeviceDataMapper deviceDataMapper;
//    @Autowired(required = false)
//    private DeviceDataHistoryMapper deviceDataHistoryMapper;
//
//    //无参构造函数
//    public DeviceDataMigration() {
//    }
//
//    //有参构造函数
//    public DeviceDataMigration(DeviceDataMapper deviceDataMapper,
//                               DeviceDataHistoryMapper deviceDataHistoryMapper) {
//        this.deviceDataMapper = deviceDataMapper;
//        this.deviceDataHistoryMapper = deviceDataHistoryMapper;
//    }
//    @Transactional
//    public void zxw22() {
//
//        if (deviceDataMapper == null || deviceDataHistoryMapper == null) {
//            deviceDataMapper = SpringUtils.getBean(DeviceDataMapper.class);
//            deviceDataHistoryMapper = SpringUtils.getBean(DeviceDataHistoryMapper.class);
//        }
//
//
//        try {
//            LocalDateTime oneMinuteAgo = LocalDateTime.now().minusMinutes(2);
//
//            List<DeviceData> oldDataList = deviceDataMapper.selectByAlarmTimeBefore(oneMinuteAgo);
//
//            deviceDataHistoryMapper.copyFromDeviceData(oneMinuteAgo);
//
//            deviceDataMapper.deleteOldData(oneMinuteAgo);
//        } catch (Exception e) {
//            throw new RuntimeException("数据迁移任务执行失败", e);
//        }
//    }

